package com.top.dao;

import com.top.entiy.Food;
import com.top.entiy.Order;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:
 * @DateTime: 2023/12/10 09:09
 * @Description: TODO
 */
public class OrderDao extends AbstractDAO implements IDAO<Integer, Order> {

    /**
     * 查询当前服务中桌号订单数量
     * @return
     */
    public int countByTableNoActive (Integer tableNo){
        try {
            String sql = "select count(*) from `order` where table_no =? and `status` = 1";
            statement = conn.prepareStatement(sql);
            statement.setInt(1, tableNo);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return 0;
    }

    @Override
    public boolean doCreate(Order vo)  {
        return false;
    }
    public Integer createReturnKey(Order vo){
        String sql = "INSERT INTO `ordering_food`.`order` " +
                "( `sn`, `table_no`, `status`, `amount`, `create_time`, `update_time`) " +
                "VALUES ( ?, ?, ?, ?, now(),now());";
        try {
            statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1,vo.getSn());
            statement.setInt(2,vo.getTableNo());
            statement.setInt(3,vo.getStatus());
            statement.setBigDecimal(4,vo.getAmount());
            statement.executeUpdate();
            ResultSet resultSet = statement.getGeneratedKeys();
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            System.err.println("创建失败"+e);
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public boolean doUpdate(Order vo) throws SQLException {
        return false;
    }

    @Override
    public boolean doRemove(Integer id) throws SQLException {
        return false;
    }

    @Override
    public Order findById(Integer id) {
        try {
            String sql = "SELECT * FROM `order` WHERE id=?";
            statement =conn.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setSn(rs.getString("sn"));
                order.setTableNo(rs.getInt("table_no"));
                order.setStatus(rs.getInt("status"));
                order.setAmount(rs.getBigDecimal("amount"));
                order.setCreateTime(rs.getDate("create_time"));
                order.setUpdateTime(rs.getDate("update_time"));
                return order;
            }
        } catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return null;
    }

    @Override
    public List<Order> findAll() {
        List<Order> all = new ArrayList<>();
        try {
            String sql = "SELECT * FROM `order` ORDER BY id desc";
            statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setSn(rs.getString("sn"));
                order.setTableNo(rs.getInt("table_no"));
                order.setStatus(rs.getInt("status"));
                order.setAmount(rs.getBigDecimal("amount"));
                order.setCreateTime(rs.getDate("create_time"));
                order.setUpdateTime(rs.getDate("update_time"));
                order.setRemark(rs.getString("remark"));
                order.setPayTime(rs.getDate("pay_time"));
                all.add(order);
            }
            return all;
        } catch (SQLException e) {
            System.err.println("查询失败"+e);
        }
        return all;
    }

    @Override
    public List<Order> findAllBySplit(String column, String keyWord, Integer currentPage, Integer lineSize) throws SQLException {
        return null;
    }

    @Override
    public Integer getAllCount(String column, String keyWord) throws SQLException {
        return null;
    }

    public boolean doUpdateStatus(int id,int status) {
        try {
            String sql = "UPDATE `order` SET `status`=? ,pay_time = NOW() WHERE id = ?";
            statement =conn.prepareStatement(sql);
            statement.setInt(1,status);
            statement.setInt(2,id);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.err.println("更新失败"+e);
        }
        return false;
    }
}
